import {orderMatch} from "/@/api/dms/order";
import {getProductList} from "/@/api/dms/product";
import {ElMessage} from "element-plus";

export const orderType =  (va:number| string)=>{
  if(va == 1){return '已保存'}else if(va == 2){return '已接收'}else if(va == 0){return '草稿'}else if(va == 5){return '已取消'}else if(va == 6){return '已提交'}else if(va == 7){return '待审核'}else if(va == 8){return '已审核'}else if(va == 9){return '待客户确认'}else if(va == 10){return '订单审核中'}else if(va == 11){return '订单已付款'}else if(va == 12){return '已出库'}else if(va == 13){return '已发出'}else if(va == 14){return '部分出库'}else if(va == 15){return '部分发出'}else if(va == 16){return '已完成'}else if(va == 20){return '审核成功'}else if(va == 21){return '审核失败'}else if(va == 80){return '已发货'}else {return '--'}
}
export const customerList= (arr:any)=>{
  const arr_1 = arr.map((value:any,index:number)=>{
    const numb = Number(value.selling_price) || 0
    const orderNumber:any = Number(value.selling_price) != 0 ? (Number(value.selling_price)/(1+Number(value.tax_rate )/100)).toFixed(6) :0.00
    return{
      goods_no:value.product_code,//产品编码
      goods_name: value.product_name,//产品名称
      // spec_no:'',//商家编码
      barcode:value.barcode,// 条码
      spec_name:value.specification,//规格名称
      brand_name:value.brand,//品牌
      goods_type:value.goods_type|| '', // 产品类型
      sales_unit:value.unit_name, //销售单位
      sales_num:value.number,  //销售数量
      price:value.selling_price,//价目表原始单价
      order_price:orderNumber, //单价
      including_tax_price:value.selling_price, //含税单价
      tax_rate:value.tax_rate, //税率
      flag:value.flag,//标记
      tax:0.00, // 税额
      total_amount:0.00, //价税合计
      warehouse_no:'', //仓库编号
      warehouse_name:'', //仓库编号
      stock_org_name:'',//库存组织名称
      settle_org_name:'',//结算组织
      amount:0.00,// 金额
      remark:value.remark,
      raw_data:value,
      batch_no:'',//批次号
      expire_date:'',//效期
      batch_usable_num:0,//预计可发量
      type:value.type,
      discount_rate: value.goods_type == '正品'? 0 : 100,//折扣率
      batch_no_list:value.stock_batch?.map((i:any)=> {return {value:i.batch_no,label:i.batch_no}}),
      id:String(new Date().getTime()).slice(-6) + index ,
      children:value.children.length > 0 && customerList(value.children) || []
    }
  })
  return arr_1

}
// 数据更换
export const intersection =(arr:any,arr1:any)=>{

 const list =  arr.map((obj:any) => {
    const match = arr1.find((item:any) => item.goods_no === obj.goods_no);
    if (match) {
      return {...obj,...match, type: 1,sales_num:obj.sales_num,flag:obj.flag};
    } else {
      return {...obj, type: "无价目表"};
    }
  })
  return list
};
//计算
export const  compute = (arr:any)=>{
  const arr_1 = arr.map((value:any,index:number)=>{
    const numb = Number(value.tax_rate)/100
    // 含税价
    const order:any =  (value.including_tax_price/(1+ numb)).toFixed(6) || 0.000000
    // 税额
    const taxNumber:any =  (order * numb * Number(value.sales_num)).toFixed(2) || 0.00
    // 不含税金额
    const amountNumber:any = (order*value.sales_num).toFixed(2) || 0.00
    // 含税金额
    const totalAmountNumber:any =  (value.including_tax_price * value.sales_num ).toFixed(2)|| 0.00
    return {
      ...value,
      order_price: isNaN(order) ? 0.000000: Number(order),
      tax: isNaN(taxNumber) ?  0.00: Number(taxNumber),
      amount:isNaN(amountNumber) ? 0.00: Number(amountNumber),
      total_amount:isNaN(totalAmountNumber) ? 0.00: Number(totalAmountNumber),
    }
  })
  return arr_1
}
//赠品订单 字段替换
export const arr_giveaway= (arr:any,number:any)=>{
  const arr_1 = arr.map((value:any,index:number)=>{
    const orderNumber:any = (value.retail_price/(1+Number(value.tax_rate )/100)).toFixed(6)
    return{
      goods_no:value.number,//产品编码
      goods_name: value.name,//产品名称
      // spec_no:'',//商家编码
      barcode:value.barcode,// 条码
      spec_name:value.specification,//规格名称
      brand_name:value.assistant_name,//品牌
      goods_type:'赠品', // 产品类型
      sales_unit:value.unit_name, //销售单位
      sales_num:0,  //销售数量
      // order_price:orderNumber, //单价
      price:value.retail_price||0.00,
      order_price:0, //单价
      // including_tax_price:value.retail_price, //含税单价
      including_tax_price:0,
      tax_rate:value.tax_rate, //税率
      tax:0.00, // 税额
      total_amount:0.00, //价税合计
      warehouse_no:'', //仓库编号
      warehouse_name:'', //仓库编号
      stock_org_name:'',//库存组织名称
      settle_org_name:'',//结算组织
      amount:0.00,// 金额
      remark:'',
      discount_rate:100,//折扣率
      raw_data:value,
      batch_no:'',//批次号
      expire_date:'',//效期
      batch_usable_num:0,//预计可发量
      batch_no_list:value.stock_batch?.map((i:any)=> {return {value:i.batch_no,label:i.batch_no}}),
      id:String(new Date().getTime()).slice(-6) + index ,
    }
  })
  arr_1.forEach((obj:any) => {
    const match = number.find((item:any) => item.goods_no == obj.goods_no);
    if (match) {
      obj.sales_num = match.goods_num;
    }
  })
  return arr_1
}
// 过滤产品数据
export const filterateList = (arr:any)=>{
  const arr1= arr.filter((item:any)=>item.goods_no != '' && item.goods_type != '') || []
  return arr1
}
// 过滤 联系信息
export const filterateContactInformation = (arr:any)=>{
  const arr1= arr.filter((item:any)=>item.shipping_contact.trim() != '' && item.shipping_tel.trim() != '' && item.shipping_address != '' && item.address.trim() != '') || []
  return arr1
}
// 把二维数组转化为一维数组
export const flattenArray=(arr:any)=> {
  let result:any = [];
  const  flatten = (arr:any)=> {
    arr.forEach((item:any) => {
      result.push(item);
      if (item.children && Object.keys(item.children).length > 0) {
        flatten(Object.values(item.children));
      }
    });
  }

  flatten(arr);

  return result;
}
// 匹配赠品策略
export const machList = async (value:any,item:any)=>{
  let a:any = []
 await orderMatch({...value,detail:[...filterateList(item)]}).then(async (data:any)=>{
    if(data.code == 200 && data.data?.item_list.length > 0 ){
        const arrNumber =  data.data?.item_list.map((item:any)=>{return item.goods_no})
       await getProductList({page:1,limit:20,type:1,filter:{number:arrNumber,use_org_id:value.sales_org_id}}).then((item:any)=>{
          if(item.code == 200){
           a = arr_giveaway(item.data.list,data.data?.item_list)
          }
        })
    }
  })
  return a
}
//  一级数组转化为二级
export  const  convertToTree = (arr:any)=> {
  const nodeMap:any = {};
  const roots:any = [];

  arr.forEach((node:any) => {
    node.children = [];
    nodeMap[node.serial_number] = node;
  });

  arr.forEach((node:any) => {
    const parentIndex = getParentIndex(node.serial_number);
    if (parentIndex) {
      const parentNode = nodeMap[parentIndex];
      if (parentNode) {
        parentNode.children.push({...node,association:true,goods_type:'赠品',including_tax_price:0, tax:0.00, total_amount:0.00,amount:0.00,order_price:0,type: 1});
      }
    } else {
      roots.push(node);
    }
  });

  return roots;
}

export const getParentIndex=(index:any)=> {
if(index){
  const parts = index?.toString().split('.');
  if (parts.length === 1) {
    return null;
  }
  parts.pop();
  return parts.join('.');
}else {
  return  null
}
}
//文件下载
export const handleUploadFile = (file:any, fileName:any) => {
  // 创建blob对象
  // const blob = new Blob([file])
  // 创建a标签，通过a标签实现下载
  const dom = document.createElement('a')
  dom.download = fileName
  // dom.href = URL.createObjectURL(file)
  dom.href = file
  dom.id = 'upload-file-dom'
  dom.style.display = 'none'
  document.body.appendChild(dom)

  // 触发点击事件
  dom.click()

  // 释放资源
  URL.revokeObjectURL(dom.href)
  document.getElementById('upload-file-dom')?.remove()
}
// 字符串转化为 数组
export const objectArray = (va:any)=>{
  if(va !== null){
    return va.split(',')
  }else {
    return []
  }

}
//阿拉伯文转化中文
export  const numberAudit= (va:any)=>{
  if(va == 1){
    return '一'
  }else if(va == 2){
    return '二'
  }else if(va == 3){
    return '三'
  }else if(va == 4){
    return '四'
  }else if(va == 5){
    return '五'
  }else if(va == 6){
    return '六'
  }else if(va == 7){
    return '七'
  }else if(va == 8){
    return '八'
  }else if(va == 9){
    return '九'
  }else if(va == 10){
    return '十'
  }else if(va == 11){
    return '十一'
  }else if(va == 12){
    return '十二'
  }else {
    return '--'
  }
}
export const timeTab = (time:any)=>{
  if(time != 0){
    return time
  }else {
    return  '--'
  }
}
export const productFields:any = {id:0,price:0.00,price_remark:'',discount_rate:0,goods_no:'',batch_usable_num:0,batch_no:'',expire_date:'',amount:0.00,goods_name:'',barcode:'',spec_name:'',brand_name:'',goods_type:'',sales_unit:'',sales_num:0,order_price:'',including_tax_price:0.00,tax_rate:'',tax:'',total_amount:0.00,warehouse_no:'',warehouse_name:'',stock_org_name:'',settle_org_name:'',remark:'',batch_no_list:[]}
// const newObject = {id:String(new Date().getTime()).slice(-6),price_remark:'',price:0.00,sales_num:0,goods_no:'',batch_usable_num:0,batch_no:'',expire_date:'',amount:0.00,goods_name:'',barcode:'',spec_name:'',brand_name:'',goods_type:'',sales_unit:'',sales_num:0,order_price:'',including_tax_price:0.00,tax_rate:'',tax:'',total_amount:0.00,warehouse_no:'',warehouse_name:'',stock_org_name:'',settle_org_name:'',remark:'',batch_no_list:[]}
 export  const arr_map= (arr:any)=>{
  const arr_1 = arr.map((value:any,index:number)=>{
    const orderNumber:any = (value.price/(1+Number(value.tax_rate )/100)).toFixed(6)
    return{
      goods_no:value.number,//产品编码
      goods_name: value.name,//产品名称
      // spec_no:'',//商家编码
      barcode:value.barcode,// 条码
      spec_name:value.specification,//规格名称
      brand_name:value.assistant_name,//品牌
      sales_unit:value.unit_name, //销售单位
      sales_num:0,  //销售数量
      order_price:orderNumber, //单价
      price:value.price,
      including_tax_price:value.price, //含税单价
      tax_rate:value.tax_rate, //税率
      tax:0.00, // 税额
      total_amount:0.00, //价税合计
      warehouse_no:'', //仓库编号
      warehouse_name:'', //仓库编号
      stock_org_name:'',//库存组织名称
      settle_org_name:'',//结算组织
      amount:0.00,// 金额
      remark:'',
      raw_data:value,
      goods_type:'正品', // 产品类型
      batch_no:'',//批次号
      expire_date:'',//效期
      batch_usable_num:0,//预计可发量
      price_remark:'',
      discount_rate:0,
      batch_no_list:value.stock_batch?.map((i:any)=> {return {value:i.batch_no,label:i.batch_no}}),
      id:String(new Date().getTime()).slice(-6) + index ,
    }
  })
  return arr_1
}
// 订单流转状态样式
export const colorStatus = (value:any)=>{
  if(value == '已取消' ){
    return '#ff8a80'
  }else if(value == '已完成' ){
    return '#43a047'
  }else if(value == '待审核' ){
    return '#ff9100'
  }else if(value == '已付款待发货' ){
    return '#fbc02d'
  }else if(value == '已发出' ){
    return '#057bf5'
  }else if(value == '草稿' ){
    return '#78909c'
  }else if(value == '已审核' ){
    return '#fb8c00'
  }else if(value == '已保存' ){
    return '#f9a825'
  }else {
    return '#a1887f'
  }
}
// 客户订单字段
export const customerFields:any = {id:0, discount_rate:0,price:0.00,sales_num:0,goods_no:'',price_remark:'',flag:'',batch_usable_num:0,batch_no:'',expire_date:'',amount:0.00,goods_name:'',barcode:'',spec_name:'',brand_name:'',goods_type:'正品',sales_unit:'',order_price:'',including_tax_price:0.00,tax_rate:'',tax:'',total_amount:0.00,warehouse_no:'',warehouse_name:'',stock_org_name:'',settle_org_name:'',remark:'',batch_no_list:[]}
export const customerMap= (arr:any)=>{
  const arr_1 = arr.map((value:any,index:number)=>{
    const orderNumber:any = (value.price/(1+Number(value.tax_rate )/100)).toFixed(6)
    return{
      goods_no:value.number,//产品编码
      goods_name: value.name,//产品名称
      // spec_no:'',//商家编码
      barcode:value.barcode,// 条码
      spec_name:value.specification,//规格名称
      brand_name:value.assistant_name,//品牌
      goods_type:'正品', // 产品类型
      sales_unit:value.unit_name, //销售单位
      sales_num:0,  //销售数量
      price:value.price || 0.00,
      order_price:isNaN(orderNumber) ?  0.00: Number(orderNumber), //单价
      including_tax_price:value.price, //含税单价
      tax_rate:value.tax_rate, //税率
      tax:0.00, // 税额
      total_amount:0.00, //价税合计
      warehouse_no:'', //仓库编号
      warehouse_name:'', //仓库编号
      stock_org_name:'',//库存组织名称
      settle_org_name:'',//结算组织
      amount:0.00,// 金额
      remark:'',
      raw_data:value,
      type:'存在价目表',
      batch_no:'',//批次号
      expire_date:'',//效期
      batch_usable_num:0,//预计可发量
      batch_no_list:value.stock_batch?.map((i:any)=> {return {value:i.batch_no,label:i.batch_no}}),
      id:String(new Date().getTime()).slice(-6) + index ,
    }
  })
  return arr_1
}
